//
//  CharmViewController.m
//  Tongcheng
//
//  Created by   丛熠超 on 2017/4/6.
//  Copyright © 2017年 Tongcheng. All rights reserved.
//

#import "CharmViewController.h"
#import "CharmTableViewCell.h"
#import "TuhaoOperator.h"
#import "TuhaoModel.h"
#import "TuhaoDataModel.h"
#import "TuhaoListModel.h"
#import "PaiHangView.h"

@interface CharmViewController ()<UITableViewDelegate, UITableViewDataSource,PaiHangViewDelegate>
@property (weak, nonatomic) IBOutlet UIButton *paihangBtn;
@property (weak, nonatomic) IBOutlet UILabel *GiftNumLB;
@property (weak, nonatomic) IBOutlet UITableView *CharmTableVIew;

@property (nonatomic, strong) NSArray *dataArray;

@property (nonatomic, assign) BOOL isThisWeek;

@property (nonatomic, strong) PaiHangView *paihang;

@end

@implementation CharmViewController

static NSString *mainCellIdentifier = @"MainTableViewCell";

- (void)setDataArray:(NSArray *)dataArray
{
    _dataArray = dataArray;
    [self.CharmTableVIew reloadData];
}

- (void)setupView
{
    _CharmTableVIew.delegate = self;
    _CharmTableVIew.dataSource = self;
    self.CharmTableVIew.tableFooterView = [[UIView alloc] init];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupView];
    self.isThisWeek = YES;
    [self download];
    // Do any additional setup after loading the view from its nib.
}
- (IBAction)ChangePaiMing:(id)sender {
    self.paihang = [PaiHangView gainMineHeaderView];
    UIWindow * window = [[UIApplication sharedApplication].windows lastObject];
    self.paihang.isThisWeek = self.isThisWeek;
    self.paihang.delegate = self;
    [window addSubview:self.paihang];
}


-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    CharmTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:mainCellIdentifier];
    if (!cell)
    {
        cell = [[UINib nibWithNibName:@"CharmTableViewCell" bundle:nil] instantiateWithOwner:self options:nil].lastObject;
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    TuhaoListModel *model = self.dataArray[indexPath.row];
    cell.model = model;
    return cell;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataArray.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 100;
}

- (void)download
{
    NSMutableDictionary *dic = [NSMutableDictionary dictionary];
    dic[@"token"] = self.config.token;
    dic[@"user_id"] = self.config.userId;
    if (self.isThisWeek == YES) {
        dic[@"Week"] = @"1";
    }else{
        dic[@"Week"] = @"2";
    }
    dic[@"Type"] = @"rec";
    TuhaoOperator *operator = [[TuhaoOperator alloc] initWithParams:dic];
    [self.networkingManager asyncTaskOnVC:self operator:operator successBlock:^(BaseModel *baseModel) {
        if ([baseModel.code isEqualToString:@"1"]) {
            self.dataArray = operator.data.data.list;
            self.GiftNumLB.text = [NSString stringWithFormat:@"我送出的礼物：%@", operator.data.data.amount];
        }
    } failBlock:^(BaseModel *baseModel) {
        
    }];
}

- (void)ThisWeek {
    self.isThisWeek = YES;
    [self.paihang removeFromSuperview];
    [self.paihangBtn setTitle:@"本周排名" forState:UIControlStateNormal];
    [self download];
}

- (void)LastWeek {
    self.isThisWeek = NO;
    [self.paihang removeFromSuperview];
    [self.paihangBtn setTitle:@"上周排名" forState:UIControlStateNormal];
    [self download];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
